<?php
class UsersController extends AppController {
	
	var $name = 'Users';
	var $helpers = array('Html', 'Form');
	

	function beforeFilter() {
		parent::beforeFilter(); 		
		$this->Auth->allow('*');
	}
	
	function welcome(){
		$this->layout = 'admin';
	}

	function admin_index() {
		$this->User->recursive = 0;
		//根据用户名搜索
		$username = Sanitize::clean(Sanitize::paranoid($this->params['form']['username']));
		debug($username, true);
		//exit;
		if(!isset($username) || empty($username)){
			$username = "%";
		}		
		$data = $this->paginate('User', array('User.username LIKE' => '%'.$username.'%' ));			
		$this->set('users', $data);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid User', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			// Check if their permission group is changing 
			$oldgroupid = $this->User->field( 'group_id' ); 
			if ( $oldgroupid !== $this->data[ 'User' ][ 'group_id' ]) { 
				     $aro =& $this->Acl->Aro; 
				     $user = $aro->findByForeignKeyAndModel( $this->data[ 'User' ][ 'id' ], 'User' ); 
				     //$user = $aro->find ( 'first' , array( 'Aro.foreignKey' => $this->data[ 'User' ][ 'id' ], 'Aro.model' => 'User' )); 
				     $group = $aro->findByForeignKeyAndModel( $this->data[ 'User' ][ 'group_id' ], 'Group' ); 
				     //$group = $aro->find ( 'first' , array( 'Aro.foreignKey' => $this->data[ 'User' ][ 'group_id' ], 'Aro.model' => 'Group' ));                          
				     // Save to ARO table 
				     $aro->id = $user [ 'Aro' ][ 'id' ]; 
				     $aro->save(array( 'parent_id' => $group [ 'Aro' ][ 'id' ])); 
			} 
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The User has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$groups = $this->User->Group->find('list');
		$this->set(compact('groups'));
	}
	
	function admin_paid($id = null) {
		if (!$id) {
				$this->Session->setFlash(__('Invalid id for User', true));
				$this->redirect(array('action' => 'index'));
			}
		$record = $this->User->findById($id);
		//debug($record, true);
		//exit;
		if($record['User']['paid'] == 0){
			$record['User']['paid'] = 1;
			$this->Session->setFlash(__('The User has been paid', true));
		}else{
			$record['User']['paid'] = 0;
			$this->Session->setFlash(__('The User has not been paid', true));
		}
		$this->User->save($record);		
		$this->redirect(array('action' => 'index'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for User', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->User->del($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The User could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function login() {
		    if ($this->Session->read('Auth.User')) {
		    	        $this->Session->setFlash('You are logged in!');
		    	        $this->redirect('/users/welcome', null, false);
		        }
	}      
	
	function logout(){
		$this->Session->setFlash('Good-Bye');
		$this->redirect($this->Auth->logout());
	}
	
	/*function initDB () { 
	    //$this -> Acl -> allow ( $aroAlias , $acoAlias ); 
		$group =& $this -> User -> Group ; 
		//Allow admins to everything 
		$group -> id = 1 ; 
		$this -> Acl -> allow ( $group , 'controllers' ); 
		//allow managers to posts and widgets 
		$group -> id = 2 ; 
		$this -> Acl -> deny ( $group , 'controllers' ); 
		$this -> Acl -> allow ( $group , 'controllers/Posts' ); 
		$this -> Acl -> allow ( $group , 'controllers/Widgets' ); 
		//allow users to only add and edit on posts and widgets 
		$group -> id = 3 ; 
		$this -> Acl -> deny ( $group , 'controllers' );
		$this -> Acl -> allow ( $group , 'controllers/Posts/add' );
		$this -> Acl -> allow ( $group , 'controllers/Posts/edit' );
		$this -> Acl -> allow ( $group , 'controllers/Widgets/add' ); 
		$this -> Acl -> allow ( $group , 'controllers/Widgets/edit' ); 
	} */

}
?>